package com.person.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.person.common.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author zhangfc
 * @date 2021/7/19 14:35
 */
@RestController
public class GoodsController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/getgoods/{total}")
    @SentinelResource(value = "fallback", fallback = "fallBackGetGoods",blockHandler = "blockGetGoods")
    public CommonResult getGoods(@PathVariable("total") int total) {
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity("http://nacosprovider70/getgoods", CommonResult.class);
        if (total == 0) {
            throw new RuntimeException("运行时异常");
        }
        CommonResult body = forEntity.getBody();
        return body;
    }


    public CommonResult fallBackGetGoods(int total,Throwable e) {
        return CommonResult.error("failure", e.getMessage());
    }

    public CommonResult blockGetGoods(int total, BlockException e){
        return CommonResult.error("failure",e.getMessage());
    }
}
